package vn.conghuy.talkwithme.data.livescore;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class Utils {
	public static String requestHtmlContent(String sUrl){
		URL url;
		URLConnection connection;
		StringBuilder strBuilder = new StringBuilder();
		BufferedReader reader;
		try {
			url = new URL( sUrl );
			connection = url.openConnection();
			connection.setRequestProperty("User-Agent", "Mozilla/6.0 (Windows NT 6.2; WOW64; rv:16.0.1) Gecko/20121011 Firefox/16.0.1");
	        InputStream is = url.openConnection().getInputStream();
	        reader = new BufferedReader( new InputStreamReader( is )  );

	        String line = null;
	        while((line = reader.readLine())!= null) {
	        	strBuilder.append(line).append("\n");
	        }
	        reader.close();
	        
	        return strBuilder.toString();
			
			/*url = new URL(sUrl);
			InputStream is = url.openStream();
			int ptr = 0;
			StringBuffer buffer = new StringBuffer();
			while ((ptr = is.read()) != -1) {
			    buffer.append((char)ptr);
			}
			return buffer.toString();*/
	        
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return strBuilder.toString();
		
		
	}
	
	public static int indexOfWord(String origin, String word){

		int index = origin.indexOf(word);
		char[] chArray = origin.toCharArray();
		while(index != -1){
			
			int indexBefore = index - 1;
			int indexAfter = index + word.length();
			if((index == 0 || (index > 0 && chArray[indexBefore] == ' '))
					&& (indexAfter >= origin.length() || (indexAfter < origin.length() && chArray[indexAfter] == ' '))){
				return index;
			}
			
			index = origin.indexOf(word, index + word.length());
		}
		return -1;
	}
}
